package com.typing.alarm.util;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.widget.EditText;

import com.typing.alarm.R;
import com.typing.alarm.activity.AlarmDetailActivity;
import com.typing.alarm.activity.AlarmRegisterActivity;
import com.typing.alarm.activity.TypingAlarmActivity;

public class DialogUtil {
	private static AlertDialog dialog;
	private static ProgressDialog progressDialog = null;
	
    private static String getTitle(Context context) {
    	return context.getString(android.R.string.dialog_alert_title);
    }
    
    /** 
     * Dialog Alert
     */
    public static boolean showAlert(Context context, String message) {    	    	
    	return showAlert(context, DialogUtil.getTitle(context) , message,false);
    }    
    /**
     * Dialog Alert
     */
    public static boolean showAlert(Context context, int message) {
    	return showAlert(context,android.R.string.dialog_alert_title , message, false);
    }    
    /**
     * Dialog Alert
     */
    public static boolean showAlert(Context context, String message , final boolean isFinish) {
    	return showAlert(context,DialogUtil.getTitle(context) , message,isFinish);
    }
    /**
     * Dialog Alert
     */
    public static boolean showAlert(Context context, int message , final boolean isFinish) {
    	return showAlert(context,android.R.string.dialog_alert_title , message,isFinish);
    }
    /**
     * Dialog Alert
     */
    public static boolean showAlert(Context context,int title, int message) {
    	return showAlert(context,title, message ,false);
    }
    /**
     * Dialog Alert
     */
    public static boolean showAlert(Context context,String title, String message) {
    	return showAlert(context,title , message,false);
    }
    /**
     * Dialog Alert 
     */
    public static boolean showAlert(Context context,int title, int message ,final boolean isFinish) {
        return showAlert(context, context.getString(title) , context.getString(message), isFinish);
    }    
    /**
     * Dialog Alert
     */
    public static boolean showAlert(final Context context,String title, String message, final boolean isFinish) {
        try{
        	closeProgress(context);
        	closeDialog(context);
	        dialog =new AlertDialog.Builder(context)
	        .setTitle(title)
	        .setMessage(message)
	        .setIcon(android.R.drawable.ic_dialog_alert)
	        .setCancelable(false)
	        .setPositiveButton(android.R.string.ok,
	                new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
//	                    	RootActivity.currentDialog = DIALOG_NONE;
//	                        dialog.dismiss();
//	                        if(isFinish) {
//	                        	Util.exit();
//	                        }
	                    }
	                }).show();
        }catch (Exception e) {
        	Log.e("", e.toString(), e);
		}
        return true;
    }
    
    /**
     * Dialog Confirm 
     */
    public static boolean showConfirm(final Context context,int title,int message ,final int type) {
        try{
        	closeProgress(context);
        	closeDialog(context);
        	dialog =new AlertDialog.Builder(context)
        	.setTitle(title)
	        .setMessage(message)
	        .setCancelable(false)
	        .setIcon(android.R.drawable.ic_dialog_alert)
            .setNegativeButton(android.R.string.cancel, 
	                    new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int which) {
	                	dialog.dismiss();
	                }
	            })
	        .setPositiveButton(android.R.string.ok,
	                new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                        dialog.dismiss();
	                    }
	                }).show();
        }catch (Exception e) {
        	Log.e("", e.toString(), e);
		}
        return true;
    }
    
    
    public static boolean showChooseDay(final AlarmRegisterActivity mActivity){
    	final String[] dayList = mActivity.getResources().getStringArray(R.array.day_name_array);
    	final String[] dayListKo = mActivity.getResources().getStringArray(R.array.day_name_array_ko);
    	
    	try{
    		closeProgress(mActivity);
    		closeDialog(mActivity);
    		
    		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    		builder.setTitle(R.string.day_select_popup_title);
    	
        	//builder.setSingleChoiceItems(cityListKo, 0, new DialogInterface.OnClickListener() 
    		builder.setMultiChoiceItems(dayList, mActivity.getRepeatDay(), new DialogInterface.OnMultiChoiceClickListener(){
				
				@Override
				public void onClick(DialogInterface dialog, int which,
						boolean isChecked) {
					// TODO Auto-generated method stub
					//dialog.dismiss();
					mActivity.setRepeatDay(which, isChecked);					
				}
			})
        	.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {					
					dialog.dismiss();
				}
			});
			dialog = builder.show();
			
    	}catch (Exception e) {
    		Log.e("", e.toString(), e);
    	}
    	
    	return true;
    }
    
    public static boolean showChooseDay(final AlarmDetailActivity mActivity){
    	final String[] dayList = mActivity.getResources().getStringArray(R.array.day_name_array);
    	final String[] dayListKo = mActivity.getResources().getStringArray(R.array.day_name_array_ko);
    	
    	try{
    		closeProgress(mActivity);
    		closeDialog(mActivity);
    		
    		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    		builder.setTitle(R.string.day_select_popup_title);
    	
        	//builder.setSingleChoiceItems(cityListKo, 0, new DialogInterface.OnClickListener() 
    		builder.setMultiChoiceItems(dayList, mActivity.getRepeatDay(), new DialogInterface.OnMultiChoiceClickListener(){
				
				@Override
				public void onClick(DialogInterface dialog, int which,
						boolean isChecked) {
					// TODO Auto-generated method stub
					//dialog.dismiss();
					mActivity.setRepeatDay(which, isChecked);					
				}
			})
        	.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {					
					dialog.dismiss();
				}
			});
			dialog = builder.show();
			
    	}catch (Exception e) {
    		Log.e("", e.toString(), e);
    	}
    	
    	return true;
    }
    
	
    public static void showProgress(Context context){
    	try{
    		if(progressDialog!=null){
    			progressDialog.cancel();
    			progressDialog.dismiss();
    		}
    			
    		//else
    		progressDialog = new ProgressDialog(context);
    		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    		progressDialog.setMessage("Loding...");
    		progressDialog.show();
    	}catch (Exception e) {
    		Log.e("", e.toString(), e);
		}
	}
    
    public static void closeDialog(Context context){
		try {
			if (dialog != null){ 
//				dialog.cancel();
				dialog.dismiss();
			}
			dialog = null;
		} catch (Exception e) {
			Log.e("", e.toString(), e);
		}

    }
    
    public static AlertDialog getDialog() {
    	return dialog;
    }
    
    public static void closeProgress(Context context){
		try {
	    	if(progressDialog!=null){
//	    		progressDialog.cancel();
	    		progressDialog.dismiss();
	    	}
	    	progressDialog = null;
		} catch (Exception e) {
			Log.e("", e.toString(), e);
		}
    }
    
	private static void cleanUp(){
//		BookshelfActivity.cleanUp();	//bookshelf clean up ;
	}
}
